<?php

use App\Libs\Commons\MyMessage;
use App\Services\Aliyun\OssService;

if (!function_exists('MyDebuger')) {
    function MyDebuger($env = '')
    {
        if (empty($env)) {
            $env = config('app.env');
        }
        if (in_array($env, ['local', 'test'])) {
            return true;
        }
        return false;
    }
}
/**
 * @author    zhe.wang
 * @desc        唯一编码
 */
if (!function_exists('My_unique_code')) {
    function My_unique_code($prefix = '')
    {
        return md5(uniqid($prefix));
    }
}

/**
 * @param    $data array
 * @return object
 * @author    zhe.wang
 * @desc    成功返回方法
 */
if (!function_exists('My_success')) {
    function My_success($data = array())
    {
        $response = array(
            'code' => MyMessage::SUCCESS,
            'data' => $data,
            'msg' => MyMessage::getMessage(MyMessage::SUCCESS)
        );
        return response()->json($response);
    }
}

/**
 * @param int $code 错误码
 * @param string $msg 错误信息
 * @param array $data
 * @return object
 * @author    zhe.wang
 * @desc    失败返回方法
 */
if (!function_exists('My_error')) {
    function My_error($code, $msg = '', $data = [])
    {
        $response = array(
            'code' => $code,
            'data' => $data,
            'msg' => $msg ? $msg : MyMessage::getMessage($code)
        );
        return response()->json($response);
    }
}

if (!function_exists('My_gen_cache_key')) {
    function My_gen_cache_key()
    {
        $key = '';
        if (func_num_args() > 0) {
            foreach (func_get_args() as $arg) {
                if (is_array($arg) || is_null($arg) || is_bool($arg)) {
                    continue;
                }
                $key .= $key ? '::' . $arg : $arg;
            }
        }
        return $key;
    }
}

/**
 * @param    $data array
 * @return array
 * @author    zhe.wang
 * @desc        rpc成功返回方法
 */
if (!function_exists('My_rpc_success')) {
    function My_rpc_success($data = array())
    {
        $result = array(
            'code' => MyMessage::RPC_SUCCESS,
            'data' => $data,
            'msg' => MyMessage::getMessage(MyMessage::RPC_SUCCESS)
        );
        return $result;
    }
}

/**
 * @param int $code 错误码
 * @param string $msg 错误信息
 * @param array $data
 * @return object
 * @author    zhe.wang
 * @desc        rpc失败返回方法
 */
if (!function_exists('My_rpc_error')) {
    function My_rpc_error($code, $msg = "", $data = [])
    {
        $result = array(
            'code' => $code,
            'data' => $data,
            'msg' => $msg ? $msg : MyMessage::getMessage($code)
        );
        return $result;
    }
}

/**
 * 打印调试方法
 * @param $var
 */
if (!function_exists('My_p')) {
    function My_p($var)
    {
        if (is_bool($var)) {
            var_dump($var);
        } else if (is_null($var)) {
            var_dump(NULL);
        } else {
            echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>" . print_r($var, true) . "</pre>";
            //        exit;
        }
    }
}

/**
 * Desc: 输出标准Sql
 * Time: 2021/4/19 14:13
 * @param \Illuminate\Database\Query\Builder $q 查询构造器
 * @return array|string|string[]|null
 */
if (!function_exists('dumpSql')) {
    function dumpSql($q)
    {
        $sql = $q->toSql();
        //处理sql换行等
        $sql = preg_replace('/\s\s+/s', ' ', $sql);
        $sql = preg_replace('/ ,/', ',', $sql);
        foreach ($q->getBindings() as $binding) {
            // 返回带引号的字符串，过滤字符串中的特殊字符；
            $binding = is_numeric($binding) ? $binding : "'" . $binding . "'";
            $sql = preg_replace('/\?/', $binding, $sql, 1);
        }
        return $sql;
    }
}

/**
 * Desc: 输出日志
 * Author: dwf
 * Time: 2021/4/19 17:00
 * @param $msg
 * @return string
 */
if (!function_exists('echoLog')) {
    function echoLog($msg)
    {
        $now = date('Y-m-d H:i:s', time());
        echo $now . '  ' . $msg . "\n";
        return $now . '  ' . $msg . "\n";
    }
}
/**
 * 私有域图片加签
 */
if (!function_exists('Get_auth_url')) {
    function Get_auth_url($url = '')
    {
        $config = config('common.ali.oss')['private'];
        $host = $config['host'];
        $arr = parse_url($url);
        if (!empty($arr['host']) && $arr['host'] != $host) {
            $reg = '/(http):\/\/([^\/]+)/i';
            $url = preg_replace($reg, $host, $url);
            //        $url = preg_replace($arr['host'],$host,$url);
        }
        if (strpos($url, $host) === false) {
            return $url;
        }
        $bucket = $config['bucket'];
        $timeout = 3600;
        $url = str_replace($host, "", $url);
        $oss = new OssService(true);
        $retUrl = $oss->signUrl($bucket, trim($url, "/"), $timeout);
        return $host . substr($retUrl, strpos($retUrl, ".com") + 4, strlen($retUrl));
    }
}

if (!function_exists('Get_auth_img')) {
    function Get_auth_img($url = '')
    {
        $config = config('common.ali.oss')['private'];
        $host = $config['host'];
        $arr = parse_url($url);
        if (!empty($arr['host']) && $arr['host'] != $host) {
            $reg = '/(http):\/\/([^\/]+)/i';
            $url = preg_replace($reg, $host, $url);
            //        $url = preg_replace($arr['host'],$host,$url);
        }
        if (strpos($url, $host) === false) {
            return $url;
        }
        $oss = new OssService(true);
        $bucket = $config['bucket'];
        $timeout = 3600;
        $url = str_replace($host, "", $url);
        $retUrl = $oss->signUrl($bucket, trim($url, "/"), $timeout);
        $retUrl = str_replace("?", '&', $retUrl);
        $retUrl = str_replace("%3F", '?', $retUrl);
        $retUrl = str_replace("%3D", '=', $retUrl);
        return $host . substr($retUrl, strpos($retUrl, ".com") + 4, strlen($retUrl));
    }
}